Configuration cInstallerWrapper
{
    param (

	[Parameter(Mandatory)]
	[ValidateNotNullOrEmpty()]
	[PSObject]$Component
    )

    Import-DscResource -ModuleName cInstaller -Name MESF_cInstallerFromMsDeployPackage

    #Log Message
    Log "cInstallerWrapperStart_$($Component.uniqueKey)" {
        Message = "Start cInstallerWrapper for $($Component.uniqueKey)"
    } 

    #If package is ensure, download the package in case of autodownlaod else ensure the package is present on the server
    if ($Component.Ensure -eq "Present" -and $Component.packageType -ne "none")
    {

        #First Download package if server is configured for AutoDownload
        if ($Component.AutoDownload -eq $true)
        {
            #Log Message
            Log "cInstallerWrapperStartDownloadPackage_$($Component.uniqueKey)" {
                Message = "Start cInstallerWrapper download package for $($Component.uniqueKey)"
            } 
        
            File "cInstallerWrapperPackage_$($Component.uniqueKey)" {
                SourcePath=$Component.BinariesRepositoryPath
                DestinationPath=$Component.LocalPackagePath
                Checksum="SHA-512"
                Ensure=$Component.Ensure
                Force=$true
            }

            #End Message
            Log "cInstallerWrapperEndDownloadPackage_$($Component.uniqueKey)" {
                Message = "End cInstallerWrapper download package for $($Component.uniqueKey)"
            } 
        }
        else
        {
            #Package has been sent by an other way because server has no autodownload capabilities
            File "cInstallerWrapperDownloadPackage_$($Component.uniqueKey)" {
                DestinationPath=$Component.LocalPackagePath
                Ensure=$Component.Ensure
            }
        }
    }


    #Start install Message
    Log "cInstallerWrapperStartInstall_$($Component.uniqueKey)" {
        Message = "Start cInstallerWrapper install package for $($Component.uniqueKey)"
    } 

    #Second package installation
    switch($Component.packageType.ToLower())
    {
        "archive" {
            #Uncompress the Archive in the targeted Folder (TODO : How manage clean folder before)
            Archive "UncompressArchive_$($Component.uniqueKey)" {
                Ensure = $Component.Ensure
                Path=$Component.LocalPackagePath
                Destination = $Component.Destination
                Checksum = "ModifiedDate"
                Validate = $true
                Force = $true
            }

            #Log Message
            Log "cInstallerWrapperEndInstall_$($Component.uniqueKey)" {
                Message = "End cInstallerWrapper uncompress package for product identified by '$($Component.uniqueKey)'"
            } 

        }
        "package" {
            #Install package
            Package "cInstallerFromPackageInstall_$($Component.uniqueKey)" {
                Ensure = $Component.Ensure
                Name = $Component.Name
                Path = $Component.LocalPackagePath
                ProductId = $Component.ProductId
                Arguments = $Component.Arguments
            }

            #End install Message
            Log "cInstallerWrapperEndInstall_$($Component.uniqueKey)" {
                Message = "End cInstallerWrapper install package for product identified by '$($Component.uniqueKey)'"
            } 

        }
        "MsDeployPackage" {
            #Install package
            cInstallerFromMsDeployPackage "cInstallerFromMSDeployPackage_$($Component.uniqueKey)" {
                Ensure = $Component.Ensure
                Path = $Component.LocalPackagePath
                Destination=$Component.Destination
            }

            #End install Message
            Log "cInstallerWrapperEndInstall_$($Component.uniqueKey)" {
                Message = "End cInstallerWrapper install MSDeploypackage for product identified by '$($Component.uniqueKey)'"
            } 

        }
        "war" {
        <#
            File "cInstallerWrapperInstallWar_$($Component.uniqueKey)" {
                SourcePath=$Component.Package.LocalPath
                DestinationPath=$Component.Package.Destination
                Checksum="SHA-512"
                Ensure=$Component.Ensure
                Force=$true
            }
        #>
        }
        "none" {
            #End install Message
            Log "cInstallerWrapperEndInstall_$($Component.uniqueKey)" {
                Message = "End cInstallerWrapper no action on component type 'none' for product identified by '$($Component.uniqueKey)'"
            } 
        }
        default {
            throw new-object System.Exception "Component type $($Component.packageType) is not implemented in cInstallerWrapper"
        }
    }

    if ($Component.Ensure -ne "Present")
    {
        #Log Message
        Log "cInstallerWrapperStartRemoveBinary_$($Component.uniqueKey)" {
            Message = "Start cInstallerWrapper remove binary package for $($Component.uniqueKey)"
        }

        File "cInstallerWrapperDownloadPackage_$($Component.uniqueKey)" {
            DestinationPath=$Component.Package.LocalPath
            Ensure=$Component.Ensure
        }        

        #Log Message
        Log "cInstallerWrapperEndRemoveBinary_$($Component.uniqueKey)" {
            Message = "End cInstallerWrapper remove binary package for $($Component.uniqueKey)"
        }

    }

    #Log Message
    Log "cInstallerWrapperEnd$($Component.uniqueKey)" {
        Message = "End cInstallerWrapper for $($Component.uniqueKey)"
    } 

}